package com.usian.article.controller.v1;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.apis.article.AuthorControllerApi;
import com.usian.article.service.ApAuthorService;
import com.usian.model.article.pojos.ApAuthor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/author")
public class ApAuthorController implements AuthorControllerApi {

    @Autowired
    private ApAuthorService apAuthorService;



    @GetMapping("/findByUserId/{id}")
    @Override
    public ApAuthor findByUserId(@PathVariable Integer id) {
        LambdaQueryWrapper<ApAuthor> apAuthorLambdaQueryWrapper =
                Wrappers.<ApAuthor>lambdaQuery ().eq (ApAuthor::getUserId, id);
        List<ApAuthor> apAuthorList = apAuthorService.list (apAuthorLambdaQueryWrapper);
        if (apAuthorList!=null && !apAuthorList.isEmpty ()){
            return apAuthorList.get (0);
        }
        return null;
    }

    @PostMapping("/save")
    @Override
    public boolean save(@RequestBody ApAuthor apAuthor) {
        return apAuthorService.save (apAuthor);
    }

    @GetMapping("/findByName/{name}")
    @Override
    public ApAuthor selectAuthorByName(@PathVariable String name) {
        LambdaQueryWrapper<ApAuthor> queryWrapper = Wrappers.<ApAuthor>lambdaQuery ().eq (ApAuthor::getName, name);
        return apAuthorService.getOne (queryWrapper);
    }

    @GetMapping("/one/{id}")
    @Override
    public ApAuthor findById(@PathVariable("id") Integer id) {
        return apAuthorService.getById(id);
    }
}
